package com.meida.module.bus.provider.handler;

import com.meida.common.base.entity.EntityMap;
import com.meida.common.base.utils.FlymeUtils;
import com.meida.common.mybatis.interceptor.GetInterceptor;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.module.admin.client.entity.BaseUser;
import com.meida.module.admin.provider.service.BaseUserService;
import com.meida.module.bus.client.entity.BusAfterservice;
import com.meida.module.bus.client.entity.BusOrder;
import com.meida.module.bus.client.entity.BusReason;
import com.meida.module.file.provider.enums.FileGroupEnum;
import com.meida.module.file.provider.service.SysFileService;
import com.meida.module.user.client.entity.AppUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 订单售后详情
 *
 * @author zyf
 */
@Component
public class BusAfterServiceGetHandler implements GetInterceptor {

    @Autowired
    private BaseUserService baseUserService;

    @Autowired
    private SysFileService sysFileService;

    @Override
    public void prepare(CriteriaQuery cq, EntityMap entityMap) {
        cq.clear();
        cq.select(BusAfterservice.class, "afterServiceId", "orderId", "reasonId", "afterDescribe", "state", "reviewerId", "auditMind", "auditTime", "createTime");
        cq.select(BusReason.class, "reasonTitle");
        cq.select(AppUser.class, "userName");
        cq.select(BusOrder.class, "orderNo");
        cq.eq(BusAfterservice.class, "afterServiceId");
        cq.createJoin(BusOrder.class).setMainField("orderId").setJoinField("orderId");
        cq.createJoin(BusReason.class).setMainField("reasonId").setJoinField("reasonId");
        cq.createJoin(AppUser.class).setMainField("userId").setJoinField("userId");

    }

    @Override
    public void complete(CriteriaQuery cq, EntityMap map) {
        Long reviewerId = map.getLong("reviewerId");
        BaseUser baseUser = baseUserService.getById(reviewerId);
        map.put("reviewerUserName", "");
        if (FlymeUtils.isNotEmpty(baseUser)) {
            map.put("reviewerUserName", baseUser.getUserName());
        }
        Long afterServiceId = map.getLong("afterServiceId");
        List<EntityMap> fileList = sysFileService.selectFileListByFileGroup(afterServiceId, BusAfterservice.class.getSimpleName(), FileGroupEnum.Image.getValue());
        map.put("fileList", fileList);
    }
}
